package com.zyz.system.manager;

import com.zyz.common.core.constants.CacheConstants;
import com.zyz.common.redis.service.RedisService;
import com.zyz.system.model.user.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class UserCacheManager {

    @Autowired
    private RedisService redisService;


    public void refreshUserStatus(Long userId , Integer status) {
        String userKey = getUserKey(userId);
        User user = redisService.getCacheObject(userKey, User.class);
        if (user == null ) {
            return;
        }
        user.setStatus(status);
        redisService.setCacheObject(userKey, user);
        // 设置有效期10分钟
        redisService.expire(userKey, 10, TimeUnit.MINUTES);
    }

    private String getUserKey(Long userId) {
        return CacheConstants.USER_DETAIL + userId;
    }


}
